package org.example.petadoption.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.example.petadoption.entity.MerchantInfoDTO;
import org.example.petadoption.entity.ResultBody;
import org.example.petadoption.entity.User;
import org.example.petadoption.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author PetAdoption
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;


    //查询表
    @PostMapping("/selectAllUser")
    public ResultBody selectAllUser(){

        List<User> sau = userService.list();
        return ResultBody.success("查询到" + sau.size() + "条数据", sau);

    }

    //用户注册
    @PostMapping("/addUser")
    public ResultBody addUser(@RequestBody User user){
        if (user == null){
            return ResultBody.fail("添加失败");
        }
        if (user.getRole() != null && user.getRole() == 1){
            return ResultBody.fail("不可以添加管理员");
        }
        if (StringUtils.isBlank(user.getUsername())){
            return ResultBody.fail("检查用户名，不能为空");
        }
        if (StringUtils.isBlank(user.getPassword())){
            return ResultBody.fail("检查密码，不能为空");
        }

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, user.getUsername());
        User user1 = userService.getOne(wrapper);
        if (user1 != null){
            return ResultBody.success("用户名已存在");
        }

        boolean result = userService.save(user);
        return result?ResultBody.success():ResultBody.fail();
    }

    //用户登陆
    @PostMapping("/login")
    public ResultBody login(String username, String password){
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)){
            return ResultBody.fail("账号和密码不能为空");
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username).eq(User::getPassword, password).eq(User::getStatus, 1);
        User user = userService.getOne(wrapper);
        if (user == null){
            return ResultBody.fail("用户不存在,或已被封禁");
        }
        return ResultBody.success("登陆成功", user);
    }



    //根据用户id修改密码
    @PostMapping("/updatepass")
    public ResultBody setPass(int id, String password){
        if (id <= 0){
            ResultBody.fail("id不合法");
        }
        if (StringUtils.isEmpty(password)){
            ResultBody.fail("修改的密码不能为空");
        }
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId, id).set(User::getPassword, password);
        boolean result = userService.update(wrapper);
        return result?ResultBody.success("密码修改成功"):ResultBody.fail("密码修改失败");
    }



    //根据用户id修改用户昵称和头像
    @PostMapping("/setuserimage")
    public ResultBody setPass(int id, String username, String avatar){

        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();

        if (id <= 0){
            ResultBody.fail("id不合法");
        }

        if (!StringUtils.isEmpty(username) || StringUtils.isEmpty(avatar)){
            wrapper.eq(User::getId,id).set(User::getUsername, username);
            boolean result = userService.update(wrapper);
            return result?ResultBody.success("昵称修改成功"):ResultBody.fail("昵称修改失败");
        }

        if (StringUtils.isEmpty(username) || !StringUtils.isEmpty(avatar)){
            wrapper.eq(User::getId,id).set(User::getAvatar, avatar);
            boolean result = userService.update(wrapper);
            return result?ResultBody.success("头像修改成功"):ResultBody.fail("头像修改失败");
        }

        return ResultBody.fail("头像和昵称必须传入一项");
    }


    //商家个人页面信息
    @PostMapping("/merchantInfo")
    public ResultBody getMerchantInfo(int id) {
        MerchantInfoDTO info = userService.getMerchantInfoByUserId(id);
        if (info == null) {
            return ResultBody.fail("未找到商家信息");
        }
        return ResultBody.success("查询成功", info);
    }




}
